package cn.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yunhe.biz.IMessageBiz;
import cn.yunhe.biz.impl.MessageBizImpl;
import cn.yunhe.model.UserInfo;
import cn.yunhe.model.message.Message;
import cn.yunhe.model.message.MessageToUser;
import cn.yunhe.model.message.MessageType;

public class addMessageServlet extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession();
		
		String tilte=request.getParameter("title");
		String content=request.getParameter("content");
		
		int messagetypeid=Integer.parseInt(request.getParameter("messagetypeid"));
		MessageType mt=new MessageType();//创建消息类型对象
		mt.setMessagetypeid(messagetypeid);
		
		
		UserInfo user=(UserInfo)session.getAttribute("user");//创建者
		String begintime=request.getParameter("begintime");
		String endtime=request.getParameter("endtime");
		Message message=new Message();
		message.setTitle(tilte);
		message.setContent(content);
		message.setMessagetype(mt);
		message.setUserinfo(user);
		message.setIfpublish(1);//是否发布属性
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date date=new Date();
		String datetime=sdf.format(date);
		message.setRecordtime(datetime);
		message.setBegintime(begintime);
		message.setEndtime(endtime);
		IMessageBiz imb=new MessageBizImpl();
		int messageid=imb.addMassage(message);
		if(messageid>0){
			//赋值发给谁的表
			int touserid=Integer.parseInt(request.getParameter("touserid"));
			MessageToUser mtu=new MessageToUser();
			mtu.setUserid(touserid);
			mtu.setMessageid(messageid);
			mtu.setIfread(0);
			out.println("<script>alert('保存成功')</secript>");
		}else{
			out.println("#1");
		}
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
		
	}
}
